package com.learning.servlet2x.response;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;


/**
 * ClassName: IdentityServlet
 * Description: 图片验证码
 * 1、配置 Web.xml
 * 2、调用(PostMan) http://localhost:8080/identityServlet
 * Date: 2014-9-27 10:30 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class IdentityServlet extends HttpServlet {

    private static final long serialVersionUID = -479885884254942306L;

    /**
     * 随机字符字典
     */
    public static final char[] CHARS = {'2', '3', '4', '5', '6', '7', '8', '9', 'A',
            'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
            'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U',
            'V', 'W', 'X', 'Y', 'Z'};

    public static Random random = new Random();


    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置输出类型
        response.setContentType("image/jpeg");
        // 获取随机字符串
        String randomString = getRandomString();
        // 放入session
        request.getSession(true).setAttribute("randomString", randomString);
        // 图片宽度
        int width = 100;
        // 图片高度
        int height = 30;
        // 随机颜色，用于背景色
        Color color = getRandomColor();
        // 反色，用于前景色
        Color reverse = getReverseColor(color);
        //创建一个彩色图片
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 获取绘图对象
        Graphics2D g = bi.createGraphics();
        //设置字体
        g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
        //设置颜色
        g.setColor(color);
        // 绘制背景
        g.fillRect(0, 0, width, height);
        //设置颜色
        g.setColor(reverse);
        //绘制随机字符
        g.drawString(randomString, 18, 20);

        //画最多100个噪音点
        for (int i = 0, n = random.nextInt(100); i < n; i++) {
            g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);
        }

        // 转成JPEG格式
        ServletOutputStream out = response.getOutputStream();
        //编码器
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        // 对图片进行编码
        encoder.encode(bi);
        out.flush();
    }

    /**
     * 获取 6 位 随机数
     *
     * @return
     */
    public static String getRandomString() {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < 6; i++) {
            // 每次取一个随机字符
            buffer.append(CHARS[random.nextInt(CHARS.length)]);
        }
        return buffer.toString();
    }

    /**
     * 获取随机的颜色
     *
     * @return
     */
    public static Color getRandomColor() {
        return new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
    }

    /**
     * 返回某颜色的反色
     *
     * @param c
     * @return
     */
    public static Color getReverseColor(Color c) {
        return new Color(255 - c.getRed(), 255 - c.getGreen(), 255 - c.getBlue());
    }

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(getRandomString());
    }
}